package com.czw.consumer.listeners;

import com.rabbitmq.client.AMQP;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

@Slf4j
@Component
public class MqListener {
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String msg) {
        System.out.println("消费者收到了simple.queue的消息：【"+msg+"】");
        throw new RuntimeException("出异常了");
    }
    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1收到了work.queue1的消息：【"+msg+"】");
        Thread.sleep(20);
    }
    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue2(String msg) throws InterruptedException {
        System.err.println("消费者2收到了work.queue2的消息......：【"+msg+"】");
        Thread.sleep(200);
    }
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg){
        System.out.println("消费者1收到了fanout.queue1的消息：【"+msg+"】");
    }
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg){
        System.err.println("消费者2收到了fanout.queue2的消息......：【"+msg+"】");
    }
    @RabbitListener(queues = "direct.queue1")
    public void listenDirectQueue1(String msg){
        System.out.println("消费者1收到了direct.queue1的消息：【"+msg+"】");
    }
    @RabbitListener(queues = "direct.queue2")
    public void listenDirectQueue2(String msg){
        System.err.println("消费者2收到了direct.queue2的消息......：【"+msg+"】");
    }
    @RabbitListener(queues = "topic.queue1")
    public void listenTopicQueue1(String msg){
        System.out.println("消费者1收到了topic.queue1的消息：【"+msg+"】");
    }
    @RabbitListener(queues = "topic.queue2")
    public void listenTopicQueue2(String msg){
        System.err.println("消费者2收到了topic.queue2的消息......：【"+msg+"】");
    }
    @RabbitListener(queues = "fanout.queue3")
    public void listenFanoutQueue3(String msg){
        System.out.println("消费者3收到了fanout.queue1的消息：【"+msg+"】");
    }
    @RabbitListener(queues = "fanout.queue4")
    public void listenFanoutQueue4(String msg){
        System.err.println("消费者4收到了fanout.queue2的消息......：【"+msg+"】");
    }
    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(name="direct.queue3",durable = "true"),
            exchange = @Exchange(name = "czw.direct2",type = ExchangeTypes.DIRECT),
            key={"green"}
    ))
    public void listenDirectQueue3(String msg){
        System.out.println("消费者3收到了direct.queue3的消息：【"+msg+"】");
    }
    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(name="direct.queue4",durable = "true"),
            exchange = @Exchange(name = "czw.direct2",type = ExchangeTypes.DIRECT),
            key={"green","pink"}
    ))
    public void listenDirectQueue4(String msg){
        System.out.println("消费者4收到了direct.queue4的消息：【"+msg+"】");
    }
    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue(Map<String,Object> msg){
        System.out.println("消费者收到了object.queue的消息：【"+msg+"】");
    }
}
